#!/usr/bin/python

import os
from sys import argv


def copiar(path, patron, path_to):

	res= []

	if path_to.rfind("/") != len(path_to)-1:
		path_to+= "/"

	for root, dirs, files in os.walk(path):
		base_file_name= root[root.rfind("/")+1:]
		if root.rfind("/") != len(root) -1 :
			root+= "/"

		for file in files:
			if file.find(patron) >= 0:
				
				os.system("cp " + root + file + " " + path_to + base_file_name + "__" + file)

	return res	

		

def ls(cmd):
	resultados, res_command= comando(cmd)
	resultados= resultados.split()
	return (resultados, res_command)

def comando(cmd):
	res_command= os.system(cmd + ">tmp_var_static_python_pepe_loco")
	stream= open("tmp_var_static_python_pepe_loco", "r")
	ret= stream.read()
	stream.close()
	return (ret, res_command)



#try:
copiar(argv[1], argv[2], argv[3])
#except:
#	print "error!"
#	print "uso: " + argv[0] + " path patron path"


